package constellation.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

import constellation.Activity.constellationImageActivity;

public class StartPagerAdapter extends PagerAdapter {
    Context context;
    List<ImageView>list;

    public StartPagerAdapter(Context context, List<ImageView> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        final int i = position % list.size();
        ImageView view = list.get(i);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("StarViewPagerAdapter.onClick" + i);
                switch (i) {
                    case 0:
                        Toast.makeText(context, "广告", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        context.startActivity(new Intent(context, constellationImageActivity.class));
                        break;
                }
            }
        });
        container.addView(list.get(position));
        return list.get(position);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(list.get(position));
    }
}
